/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.tool;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

/**
 * 封装so库引用类
 * Created by JustTong on 2018/12/6.
 */
public class JNIYcs {
    // Used to load the 'ycs' library on application startup.
    static {
        System.loadLibrary("ycs");
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringTimestamp();

    public native String stringCode();

    public native String stringTitle();

    public native String stringOk();

    public JNIYcs(Context mContext) {
        try {
            //libycs 2019-12-06 17:46:24
            long timestamp = Long.valueOf(stringTimestamp());
            long timestampNow = System.currentTimeMillis();
            String code =stringCode();
            if (timestamp < timestampNow) {
//                Toast.makeText(mContext, code, Toast.LENGTH_LONG).show();
                showNormalDialog(mContext,code);
            }
        }catch (Exception ex){
            //ex 不打印
        }
    }

    private void showNormalDialog(Context mContext,String msg){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(mContext);
        normalDialog.setTitle(stringTitle());
        normalDialog.setMessage(msg);
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton(stringOk(),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
//        normalDialog.setNegativeButton("关闭",
//                new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        //...To-do
//                    }
//                });
        // 显示
        normalDialog.show();
    }
}
